import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

import '../components/PageWrap.dart';

class WillPopScopeLn extends StatefulWidget {
  const WillPopScopeLn({super.key});

  @override
  State<WillPopScopeLn> createState() => _WillPopScopeLnState();
}

class _WillPopScopeLnState extends State<WillPopScopeLn> {
  DateTime? _lastPressedAt; //上次点击时间

  @override
  Widget build(BuildContext context) {
    return PageWrap(
      title: "导航返回拦截",
      children: <Widget>[
        WillPopScope(
          onWillPop: () async {
            if (_lastPressedAt == null ||
                DateTime.now().difference(_lastPressedAt!) >
                    Duration(seconds: 1)) {
              //两次点击间隔超过1秒则重新计时
              _lastPressedAt = DateTime.now();
              return false;
            }
            return true;
          },
          child: Container(
            alignment: Alignment.center,
            child: Text("1秒内连续按两次返回键退出"),
          ),
        )
      ],
    );
  }
}
